var app = getApp();
Page({
  data: {
    img_height: 1,
    productTypeId: 0,
    ptype:0,
    atype:0,
    list01: [
      { item_id1: 1 }, { item_id1: 11 }, { item_id1: 11 },
    ],
    paixu:0,
    // 展开折叠
    selectedFlag: [false, false, false, false],
    img: "http://file.jwsxmall.com/jiantoua.png",
    imgtype:0
  },
  onLoad: function (options) {
    let that = this
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      productType: options.productType,
      typeid: options.typeid,
      screenWidth: app.globalData.screenWidth
    })
    wx.setStorageSync("typeName1", undefined)
    console.info("手机宽度" + that.data.screenWidth)
    if (that.data.screenWidth >= 320 && that.data.screenWidth < 358) {
        that.setData({
          classificationHeight:80
        })
    }
    if (that.data.screenWidth >= 358 && that.data.screenWidth < 373) {
      that.setData({
        classificationHeight: 90
      })
    }
    if (that.data.screenWidth >= 373 && that.data.screenWidth < 412) {
      that.setData({
        classificationHeight: 95
      })
    }
    if (that.data.screenWidth >= 412 && that.data.screenWidth < 767) {
      that.setData({
        classificationHeight: 103
      })
    }
    if (that.data.screenWidth >= 768) {
      that.setData({
        classificationHeight: 150
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          img_height: res.windowHeight - app.globalData.navHeight - 100,
        })
      }
    })


    if (that.data.typeName1 == undefined || that.data.typeName1 == '') {
      wx.request({
        url: app.globalData.url + "getProductAllTypeList",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          categoryTag: that.data.typeid
        },
        method: "POST",
        success: function (res) {
          var data = res.data;

          var arr = [];
          for (var i = 0; i < data.data.length; i++) {
            arr = { "index": i };
            data.data[i].index = i;
          }
          if (data.data.length > 0) {
            that.setData({
              productTypeId: res.data.data[0].typeId,
              classification: data.data,
              option: data.data[0].typeName,
            })
          } else (
            that.setData({
              classification: data.data,
            })
          )

          wx.request({
            url: app.globalData.url + "classificationGoods",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              productTypeId: res.data.data[0].typeId,
              tagId: that.data.typeid,
              type: 0,
              userId:wx.getStorageSync("userId")
            },
            method: "POST",
            success: function (res) {
              var data = res.data;
              if (data.result === 1) {
                for (var i = 0; i < res.data.data.length; i++) {
                  if (res.data.data[i].promotionMsg != undefined) {
                    var arr = res.data.data[i].promotionMsg.split(",");
                    res.data.data[i].arr = arr;
                  } else {
                    res.data.data[i].arr = [];
                  }
                  if (res.data.data[i].promotionType != undefined) {
                    //先按照逗号分割字符串
                    let arrycode = res.data.data[i].promotionType;
                    //去重
                    arrycode = Array.from(new Set(arrycode));
                    //拼接正确的nextOperatorCode
                    res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
                    // var promotionType = res.data.data[i].promotionType.split(",");
                    // res.data.data[i].promotionType = promotionType;
                  } else {
                    res.data.data[i].promotionType = [];
                  }
                }
                console.info(res.data.data);
                that.setData({
                  parentProductType: data.data,
                  product: res.data.data,
                })
              }
            }
          });
        }
      });
    }

  }, 
  onReady: function (options) {
    
  },
  onShow:function(){
    var that = this;

    that.setData({
      typeName1: wx.getStorageSync("typeName1")
    })
    console.info(that.data.typeName1);
    
    
    that.data.selectedFlag[0]=true
    that.setData({
      selectedFlag: that.data.selectedFlag
    })
    console.info(that.data.selectedFlag);
  },
  clickTitle: function (e) {
    let that = this
    that.setData({
      option: e.currentTarget.dataset.title,
    })
  },
  find: function (e) {
    var that = this;
    console.log(e.detail.value)
    if (e.detail.value != "") {
      wx.request({
        url: app.globalData.url + "searchListByES",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          searchCondition: e.detail.value,
          type: 2,
          parentTypeId: that.data.parentTypeId
        },
        method: "POST",
        success: function (res) {
          var data = res.data;
          if (data.result === 1) {
            that.setData({
              product: data.data,
            })
          }
        }
      });
    } else {
      that.productF(that.data.productTypeId);
    }
  },
  navBack: function () {
    wx.setStorageSync("typeName1", undefined)
    wx.navigateBack({

    })
  },
  
  receive: function (e) {
    var that = this;
    console.info(JSON.stringify(this.data.product[e.target.dataset.index]));
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset.productid + "&product=" + JSON.stringify(this.data.product[e.target.dataset.index]) + '&typeName1=' + that.data.typeName1+"&type=1",
    })
  },
  // 点击tab切换view
  city: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if (this.data.type === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        type: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + '',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type
      },
      success: function (res) {
        if (that.data.type == 4) {
          that.setData({
            orderPasList: res.data.data
          })
        } else {
          that.setData({
            orderList: res.data.data
          });
        }
      },
    });
  },
  // 点击tab切换view
  choose: function (e) {
    
    var that = this;
    if (this.data.typename === e.target.dataset.typename) {
      return false;
    } else {
      that.setData({
        option: e.currentTarget.dataset.typename,
      })
    }

  },
  // 点击tab切换view
  city1: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if(this.data.atype === e.target.dataset.type) {
      if(e.target.dataset.type == 2){
        var productNew = that.data.product.reverse();
        console.log(productNew)
        that.setData({
          product:productNew
        })
        if (that.data.imgtype == 2){
          that.setData({
            img: "http://file.jwsxmall.com/jiantoua1.png",
            imgtype: 1
          })
        }else{
          that.setData({
            img: "http://file.jwsxmall.com/jiantou2.png",
            imgtype: 2
          })
        }
      }
      return false;
    } else {
      if (e.target.dataset.type == 2){
        that.setData({
          img: "http://file.jwsxmall.com/jiantou2.png",
          imgtype: 2
        })
      }else{
        that.setData({
          img: "http://file.jwsxmall.com/jiantoua.png",
          imgtype: 0
        })
      }
      that.setData({
        atype: e.target.dataset.type,
        paixu: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + "classificationGoods",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        productTypeId: that.data.productTypeId,
        tagId: that.data.typeid,
        type: that.data.paixu,
        userId: wx.getStorageSync("userId")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].promotionMsg != undefined) {
              var arr = res.data.data[i].promotionMsg.split(",");
              res.data.data[i].arr = arr;
            } else {
              res.data.data[i].arr = [];
            }
            if (res.data.data[i].promotionType != undefined) {
              //先按照逗号分割字符串
              let arrycode = res.data.data[i].promotionType;
              //去重
              arrycode = Array.from(new Set(arrycode));
              //拼接正确的nextOperatorCode
              res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
              // var promotionType = res.data.data[i].promotionType.split(",");
              // res.data.data[i].promotionType = promotionType;
            } else {
              res.data.data[i].promotionType = [];
            }
          }
          console.info(res.data.data);
          that.setData({
            parentProductType: data.data,
            product: res.data.data,
          })
        }
      }
    });
  },
  toShowNoPai:function(e){
    var that = this;
    console.info(e.currentTarget.dataset.type);
    var that = this;
    if (this.data.productType === e.currentTarget.dataset.type) {
      return false;
    } else {

      that.setData({
        productType: e.currentTarget.dataset.type,
        typeid: e.currentTarget.dataset.type
      })
      wx.request({
        url: app.globalData.url + "getProductAllTypeList",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          categoryTag: e.currentTarget.dataset.type
        },
        method: "POST",
        success: function (res) {
          var data = res.data;

          var arr = [];
          for (var i = 0; i < data.data.length; i++) {
            arr = { "index": i };
            data.data[i].index = i;
          }
          if (data.data.length > 0) {
            that.setData({
              classification: data.data,
              option: data.data[0].typeName,
              productTypeId: data.data[0].typeId,
            })
          } else {
            that.setData({
              classification: data.data,
            })
          }
          wx.request({
            url: app.globalData.url + "classificationGoods",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              productTypeId: that.data.productTypeId,
              tagId: that.data.typeid,
              type: that.data.paixu,
              userId: wx.getStorageSync("userId")
            },
            method: "POST",
            success: function (res) {
              var data = res.data;
              if (data.result === 1) {
                for (var i = 0; i < res.data.data.length; i++) {
                  if (res.data.data[i].promotionMsg != undefined) {
                    var arr = res.data.data[i].promotionMsg.split(",");
                    res.data.data[i].arr = arr;
                  } else {
                    res.data.data[i].arr = [];
                  }
                  if (res.data.data[i].promotionType != undefined) {
                    //先按照逗号分割字符串
                    let arrycode = res.data.data[i].promotionType;
                    //去重
                    arrycode = Array.from(new Set(arrycode));
                    //拼接正确的nextOperatorCode
                    res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
                    // var promotionType = res.data.data[i].promotionType.split(",");
                    // res.data.data[i].promotionType = promotionType;
                  } else {
                    res.data.data[i].promotionType = [];
                  }
                }
                console.info(res.data.data);
                that.setData({
                  parentProductType: data.data,
                  product: res.data.data,
                })
              }
            }
          });
        }
      });
      
    }
  },
  // 展开折叠选择 
  changeToggle: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var typeId = e.currentTarget.dataset.typeid;
    if (that.data.selectedFlag[index]) {
      that.data.selectedFlag[index] = false;
    } else {
      that.data.selectedFlag[index] = true;
    }
    that.setData({
      selectedFlag: that.data.selectedFlag
    })
    console.info(e.target.dataset.typename);
    if (that.data.typename === e.target.dataset.typename) {
      return false;
    } else {
      that.setData({
        option: e.currentTarget.dataset.typename,
      })
    }
   
  },
  goProduct:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var typeId = e.currentTarget.dataset.typeid;
    if (that.data.selectedFlag[index]) {
      that.data.selectedFlag[index] = false;
    } else {
      that.data.selectedFlag[index] = true;
    }
    that.setData({
      selectedFlag: that.data.selectedFlag
    })
    console.info(e.target.dataset.typename);
    if (that.data.typename === e.target.dataset.typename) {
      return false;
    } else {
      that.setData({
        option: e.currentTarget.dataset.typename,
      })
    }
    var typename = e.currentTarget.dataset.typename;
    var typeid = e.currentTarget.dataset.typeid;
    that.setData({
      typeName1:typename,
      productTypeId: typeid
    }),
    wx.request({
      url: app.globalData.url + "classificationGoods",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        productTypeId: e.currentTarget.dataset.typeid,
        tagId: that.data.typeid,
        type: that.data.paixu,
        userId: wx.getStorageSync("userId")
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        if (data.result === 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].promotionMsg != undefined) {
              var arr = res.data.data[i].promotionMsg.split(",");
              res.data.data[i].arr = arr;
            } else {
              res.data.data[i].arr = [];
            }
            if (res.data.data[i].promotionType != undefined) {
              //先按照逗号分割字符串
              let arrycode = res.data.data[i].promotionType;
              //去重
              arrycode = Array.from(new Set(arrycode));
              //拼接正确的nextOperatorCode
              res.data.data[i].promotionType = arrycode = arrycode.toLocaleString();
              // var promotionType = res.data.data[i].promotionType.split(",");
              // res.data.data[i].promotionType = promotionType;
            } else {
              res.data.data[i].promotionType = [];
            }
          }
          console.info(res.data.data);
          that.setData({
            parentProductType: data.data,
            product:res.data.data,
          })
        }
      }
    });
  },
  goFind: function () {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
})
